﻿/*
   Copyright 2012 University of Southampton

   Licensed under the Apache License, Version 2.0 (the "License");
   you may not use this file except in compliance with the License.
   You may obtain a copy of the License at

       http://www.apache.org/licenses/LICENSE-2.0

   Unless required by applicable law or agreed to in writing, software
   distributed under the License is distributed on an "AS IS" BASIS,
   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   See the License for the specific language governing permissions and
   limitations under the License.
*/

using System;
using System.Web;
using System.Drawing;
using System.IO;
using System.Windows.Media.Imaging;
using System.Windows.Media;
using System.Drawing.Imaging;
using Microsoft.SharePoint;
using System.Linq;
using uk.ac.soton.ses.DAL;
using System.Web.Script.Serialization;
using System.Collections.Generic;

namespace uk.ac.soton.ses.layouts
{
    public class ExperimentSearchHandler : IHttpHandler
    {
        /// <summary>
        /// You will need to configure this handler in the web.config file of your 
        /// web and register it with IIS before being able to use it. For more information
        /// see the following link: http://go.microsoft.com/?linkid=8101007
        /// </summary>
        #region IHttpHandler Members

        public bool IsReusable
        {
            // Return false in case your Managed Handler cannot be reused for another request.
            // Usually this would be false in case you have some state information preserved per request.
            get { return false; }
        }

        public void ProcessRequest(HttpContext context)
        {
            //Work out which searches were asked for
            string substringQuery = context.Request.QueryString["substring"];
            string experimentQuery = context.Request.QueryString["experimentname"];
            string parameterQuery = context.Request.QueryString["parametername"];
            bool doSubstringSearch = !String.IsNullOrEmpty(substringQuery);
            bool doExperimentSearch = !String.IsNullOrEmpty(experimentQuery);
            bool doParameterSearch = !String.IsNullOrEmpty(parameterQuery);

            //Check we were asked for a search
            if (!doSubstringSearch && !doExperimentSearch && !doParameterSearch)
            {
                //Bad request
                context.Response.StatusCode = 400;
                return;
            }

            //Perform the searches
            List<KeyValuePair<int, string>> results = new List<KeyValuePair<int, string>>();
            HDCConfigurationOptions config = HDCConfiguration.GetConfigurationOptions();
            SPSecurity.RunWithElevatedPrivileges(delegate()
            {
                using (MaterialsMetadataContext dbcontext = new MaterialsMetadataContext(config.DatabaseOptions.EntityConnectionString))
                {
                    if (doSubstringSearch)
                    {
                        results.AddRange(dbcontext.FindExperimentsBySubstring(substringQuery, false));
                    }
                    if (doExperimentSearch)
                    {
                        results.AddRange(dbcontext.FindExperimentsByName(experimentQuery, false));
                    }
                    if (doParameterSearch)
                    {
                        results.AddRange(dbcontext.FindExperimentsByParameterName(parameterQuery));
                    }

                    JavaScriptSerializer json = new JavaScriptSerializer();
                    context.Response.ContentType = "application/json";
                    context.Response.Output.Write(json.Serialize(results));
                }
            });
        }

        #endregion
    }
}
